<?php
/**
 * Created by PhpStorm.
 * User: pankajmishra
 * Date: 16/12/14
 * Time: 9:27 AM
 */
namespace Application\Model\Dao;

use Zend\Db\Adapter\Driver\ResultInterface;
use Zend\Db\ResultSet\ResultSet;


class FolderDao extends BaseDao {

    public static $INSERT_FOLDER = "insert into folders(parentId, title, photoset, description, createdBy) values(?,?,?,?,?)";

    public static $DELETE_FOLDER = "delete from folders where id=?";

    public static $DELETE_FOLDER_FROM_FOLDER = "delete from folders where parentId=?";

    public static $GET_FOLDER = "select * from folders where id=?";

    public static $GET_FOLDER_FROM_FOLDERS = "select * from folders where parentId=?";


    /**
     * @param $title
     * @param $desc
     * @param $photoSet
     */
    public function saveUpdateData($parentId, $title, $desc, $photoSet, $createdBy)
    {
        $connection = $this->adapter->driver->getConnection();
        $connection->beginTransaction();

        try {
            $stmt = $this->adapter->createStatement(FolderDao::$INSERT_FOLDER, array(
                $parentId,
                $title,
                $desc,
                $photoSet,
                $createdBy
            ));
            $stmt = $stmt->prepare();
            $stmt->execute();
            $stmt = null;
            $connection->commit();
        } catch (\Exception $e) {
            echo "exception";
            $connection->rollback();
        }
    }

    /**
     * @param $id
     */
    public function deleteFolders($id)
    {

        $stmt = $this->adapter->createStatement(FolderDao::$DELETE_FOLDER, array(
            $id,
        ));
        $stmt = $stmt->prepare();
        $stmt->execute();
        $stmt = null;
    }

    /**
     * @param $id
     */
    public function deleteFoldersFromFolders($id)
    {

        $stmt = $this->adapter->createStatement(UpdateDao::$DELETE_FOLDER_FROM_FOLDER, array(
            $id,
        ));
        $stmt = $stmt->prepare();
        $stmt->execute();
        $stmt = null;
    }


    /**
     * @param $id
     * @return array
     */
    public function getFolder($id){

        $stmt = $this->adapter->createStatement(FolderDao::$GET_FOLDER, array($id));

        $stmt->prepare();
        $result = $stmt->execute();
        if ($result instanceof ResultInterface && $result->isQueryResult()) {
            $resultSet = new ResultSet ();
            $resultSet->initialize($result);
            $resultArray = array();
            foreach ($resultSet as $result) {
                array_push($resultArray, $result);
            }
            return $resultArray;
        }
    }

    /**
     * @param $id
     * @return array
     */
    public function getFoldersFromFolder($id){

        $stmt = $this->adapter->createStatement(FolderDao::$GET_FOLDER_FROM_FOLDERS, array($id));

        $stmt->prepare();
        $result = $stmt->execute();
        if ($result instanceof ResultInterface && $result->isQueryResult()) {
            $resultSet = new ResultSet ();
            $resultSet->initialize($result);
            $resultArray = array();
            foreach ($resultSet as $result) {
                array_push($resultArray, $result);
            }
            return $resultArray;
        }
    }

}